// pages/Login/Login.js
const App = getApp()
const server = App.path.Server
Page({

    /**
     * 页面的初始数据
     */
    data: {
        phone: '',
        smsCode: '',
        disabled: false,
        count: 60,
        defaultMsg: '获取验证码'
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {

    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function () {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function () {

    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function () {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function () {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function () {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function () {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function () {

    },
    bindPhoneInput: function (e) {
        this.setData({
            phone: e.detail.value
        })
    },
    bindCodeInput: function (e) {
        this.setData({
            smsCode: e.detail.value
        })
    },
    sendSms: function (e) {
        let that = this;
        wx.request({
            url: server + '/v1/socialwork/sendSms',
            method: 'POST',
            data: {
                phone: that.data.phone
            },
            header: {
                'content-type': 'application/x-www-form-urlencoded' // 默认值
            },
            complete: function (res) {
                App.judgeCode(res, function (_res) {
                    let timer = setInterval(() => {
                        if (that.data.count === 0) {
                            clearInterval(timer)
                            that.setData({
                                count: 60,
                                disabled: false
                            })
                        }
                        else {
                            if (!that.data.disabled) {
                                that.setData({
                                    disabled: true
                                })
                            }
                            if (that.data.disabled) {
                                let _count = that.data.count
                                _count--
                                that.setData({
                                    count: _count
                                })
                            }
                        }
                    }, 1000 * 1)
                })
            }
        })
    },
    checkSmsCode: function () {
        let that = this;
        wx.login({
            success(res) {
                wx.request({
                    url: server + '/v1/socialwork/checkSmsCode',
                    method: 'POST',
                    data: {
                        phone: that.data.phone,
                        smsCode: that.data.smsCode,
                        code: res.code
                    },
                    header: {
                        'content-type': 'application/x-www-form-urlencoded' // 默认值
                    },
                    complete: function (res) {
                        wx.showLoading({
                            title: '登陆成功',
                        })
                        App.judgeCode(res, function (_res) {
                            wx.redirectTo({
                                url: '/pages/Home/Home',
                                success(res) {
                                    wx.hideLoading()
                                }
                            })
                        })
                    }
                })
            }
        })
    }
})